package com.zpark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zpark.entity.patients.Patients;
import com.zpark.mapper.PatientsMapper;
import com.zpark.service.PatientsService;
import com.zpark.utils.JwtUtil;
import com.zpark.utils.PasswordUtil;
import com.zpark.utils.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class PatientsServiceImpl extends ServiceImpl<PatientsMapper, Patients> implements PatientsService {

    @Override
    public ResultVO<Patients> register(Patients patient) {
        // 1. 参数校验
        if (patient == null || !StringUtils.hasText(patient.getUsername()) || !StringUtils.hasText(patient.getPassword())) {
            return ResultVO.fail(400, "用户名和密码不能为空");
        }

        // 2. 检查用户名是否已存在
        if (exists(new LambdaQueryWrapper<Patients>().eq(Patients::getUsername, patient.getUsername()))) {
            return ResultVO.fail(400, "用户名已存在");
        }

        // 3. 密码加密处理
        patient.setPassword(PasswordUtil.encrypt(patient.getPassword()));
        patient.setStatus(1); // 默认激活状态
        patient.setRole("patient");
        patient.setCreatedAt(new Date());
        patient.setUpdatedAt(new Date());

        // 4. 保存用户信息
        if (save(patient)) {
            patient.setPassword(null); // 返回时不带密码
            return ResultVO.success("注册成功", patient);
        }
        return ResultVO.fail(500, "注册失败");
    }

    @Override
    public ResultVO<String> login(String username, String password) {
        // 1. 参数校验
        if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
            return ResultVO.fail(400, "用户名和密码不能为空");
        }

        // 2. 查询用户
        Patients patient = getOne(new LambdaQueryWrapper<Patients>()
            .eq(Patients::getUsername, username));

        // 3. 验证用户
        if (patient == null || !PasswordUtil.verify(password, patient.getPassword())) {
            return ResultVO.fail(401, "用户名或密码错误");
        }

        // 4. 检查状态
        if (patient.getStatus() != 1) {
            return ResultVO.fail(403, "账号已被禁用");
        }

        // 5. 生成token
        JwtUtil JwtUtil = new JwtUtil();
        String token = JwtUtil.generateToken(patient.getPatientsId(), patient.getUsername());
        return ResultVO.success("登录成功", token);
    }

    @Override
    public ResultVO<Patients> updatePatientInfo(Patients patient) {
        // 1. 参数校验
        if (patient == null || patient.getPatientsId() == null) {
            return ResultVO.fail(400, "参数错误");
        }

        // 2. 获取原数据
        Patients existing = getById(patient.getPatientsId());
        if (existing == null) {
            return ResultVO.fail(404, "患者不存在");
        }

        // 3. 更新允许修改的字段
        if (StringUtils.hasText(patient.getRealname())) {
            existing.setRealname(patient.getRealname());
        }
        if (StringUtils.hasText(patient.getPhone())) {
            existing.setPhone(patient.getPhone());
        }
        if (StringUtils.hasText(patient.getEmail())) {
            existing.setEmail(patient.getEmail());
        }
        if (patient.getGender() != null) {
            existing.setGender(patient.getGender());
        }
        if (patient.getBirthday() != null) {
            existing.setBirthday(patient.getBirthday());
        }

        // 4. 自动填充更新时间
        existing.setUpdatedAt(new Date());

        // 5. 执行更新
        if (updateById(existing)) {
            existing.setPassword(null);
            return ResultVO.success("更新成功", existing);
        }
        return ResultVO.fail(500, "更新失败");
    }

    @Override
    public ResultVO<Void> changePassword(Integer patientsId, String oldPassword, String newPassword) {
        // 1. 参数校验
        if (patientsId == null || !StringUtils.hasText(oldPassword) || !StringUtils.hasText(newPassword)) {
            return ResultVO.fail(400, "参数错误");
        }

        // 2. 获取用户
        Patients patient = getById(patientsId);
        if (patient == null) {
            return ResultVO.fail(404, "患者不存在");
        }

        // 3. 验证旧密码
        if (!PasswordUtil.verify(oldPassword, patient.getPassword())) {
            return ResultVO.fail(401, "原密码错误");
        }

        // 4. 检查新旧密码是否相同
        if (oldPassword.equals(newPassword)) {
            return ResultVO.fail(400, "新密码不能与旧密码相同");
        }

        // 5. 更新密码
        patient.setPassword(PasswordUtil.encrypt(newPassword));
        patient.setUpdatedAt(new Date());

        if (updateById(patient)) {
            return ResultVO.success("密码修改成功");
        }
        return ResultVO.fail(500, "密码修改失败");
    }

    @Override
    public ResultVO<Void> updateStatus(Integer patientsId, Integer status) {
        // 1. 参数校验
        if (patientsId == null || status == null || (status != 0 && status != 1)) {
            return ResultVO.fail(400, "参数错误");
        }

        // 2. 获取用户
        Patients patient = getById(patientsId);
        if (patient == null) {
            return ResultVO.fail(404, "患者不存在");
        }

        // 3. 更新状态
        patient.setStatus(status);
        patient.setUpdatedAt(new Date());

        if (updateById(patient)) {
            return ResultVO.success("状态更新成功");
        }
        return ResultVO.fail(500, "状态更新失败");
    }

    @Override
    public Patients getByUsername(String username) {
        if (!StringUtils.hasText(username)) {
            return null;
        }
        return getOne(new LambdaQueryWrapper<Patients>()
            .eq(Patients::getUsername, username));
    }
}
